import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import CustomSwing.JCLabel;
import CustomSwing.JCTextField;

@SuppressWarnings("serial")
public class CreateNewVendorPanel extends JPanel {
	public String USER_FUNC = "Add New Vendor";
	private Font custFont = new Font("Arial Bold", Font.BOLD, 15);

	Db db;
	private ButtonGroup paidBG = new ButtonGroup();

	private JCTextField fNameTF = new JCTextField("");
	private JCTextField lNameTF = new JCTextField("");
	private JCTextField addressTF = new JCTextField("");
	private JCTextField phoneTF = new JCTextField("");
	private JCTextField emailTF = new JCTextField("");
	private JCTextField ubiTF = new JCTextField("");
	private JCTextField bNameTF = new JCTextField("");
	private JCTextField stallNumTF = new JCTextField("");
	private JRadioButton yesRB = new JRadioButton(); //determines memPaid
	private JRadioButton noRB = new JRadioButton(); //determines memPaid

	public CreateNewVendorPanel(Db theDB) {
		db = theDB;
		setRadioButton(yesRB, "YES");
		setRadioButton(noRB, "NO");

		JPanel formPanel = new JPanel(new GridLayout(9, 2, 0, 2));
		formPanel.add(new JCLabel("First Name"));
		formPanel.add(fNameTF);
		formPanel.add(new JCLabel("Last Name"));
		formPanel.add(lNameTF);
		formPanel.add(new JCLabel("Address"));
		formPanel.add(addressTF);
		formPanel.add(new JCLabel("Phone Number"));
		formPanel.add(phoneTF);
		formPanel.add(new JCLabel("Email"));
		formPanel.add(emailTF);
		formPanel.add(new JCLabel("UBI"));
		formPanel.add(ubiTF);
		formPanel.add(new JCLabel("Business name"));
		formPanel.add(bNameTF);
		formPanel.add(new JCLabel("Stall #"));
		formPanel.add(stallNumTF);
		formPanel.add(new JCLabel("Membership Paid?"));
		JPanel memPaidPanel = new JPanel(new GridLayout(1, 2));
		memPaidPanel.add(yesRB);
		memPaidPanel.add(noRB);
		formPanel.add(memPaidPanel);

		JPanel buttonPanel = new JPanel(new GridLayout());
		buttonPanel.add(new JPanel());
		buttonPanel.add(createButton("Add New Vendor"));
		buttonPanel.add(new JPanel());

		setLayout(new BorderLayout());
		add(formPanel);
		add(buttonPanel, BorderLayout.SOUTH);
	}

	private void addNewVendorInDB() {
		try {
			String yesNo = yesRB.isSelected() ? "YES" : "NO";
			db.createVendor(fNameTF.getText(),
					lNameTF.getText(), addressTF.getText(),
					phoneTF.getText(), emailTF.getText(), yesNo,
					ubiTF.getText(), bNameTF.getText(),
					stallNumTF.getText());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void setRadioButton(JRadioButton rb, String name) {
		rb.setFont(custFont);
		rb.setText(name);
		rb.setSelected(name.equalsIgnoreCase("NO")); //initial set
		paidBG.add(rb);
	}

	private JButton createButton(final String name) {
		final JButton button = new JButton(name);
		button.setFont(custFont);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ae) {
				if (name.equalsIgnoreCase("Add New Vendor")) {
					final int value = JOptionPane.showConfirmDialog(null,
							"Are you sure?", name, JOptionPane.OK_OPTION);
					if (value == JOptionPane.OK_OPTION) {
						if (isInteger(stallNumTF.getText())) {
							addNewVendorInDB();
							 clearAllFields();
						} else {
							JOptionPane.showConfirmDialog(null, 
								"Input an Integer number for 'Stall #'", 
								"Error", JOptionPane.CLOSED_OPTION);
						}
					}
				}
			}
		});
		return button;
	}
	
	private void clearAllFields() {
		fNameTF.setText("");
		lNameTF.setText("");
		addressTF.setText("");
		phoneTF.setText("");
		emailTF.setText("");
		ubiTF.setText("");
		bNameTF.setText("");
		stallNumTF.setText("");
		yesRB.setSelected(false);
		noRB.setSelected(true);
	}

	private boolean isInteger(String s) {
		try { Integer.parseInt(s); 
		} catch(NumberFormatException e) { return false; }
		return true;
	}
}
